<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .slider-box {
            border: 1px solid black;
            position: relative;
            height: 40px;
            width: 400px;
            /*background: linear-gradient(to right,red ,red 50%,blue 50%,blue)*/
        }

        .slider-line {
            position: absolute;
            width: 360px;
            height: 6px;
            background-color: grey;
            left: 20px;
            top: 17px;
        }

        .slider-point {
            position: absolute;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background: radial-gradient(white, black);
            left: 100px;
            top: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <input v-model.number="vol">
        <!--<slider :ratio="vol" @ratiochange="ratiochange"></slider>-->
        <!--
            v-model是一个语法糖，相当于一个v-on加上一个v-bind，v-bind绑定value属性，
            v-on监听input事件
        -->
        <!--
            自定义的组件，可以使用v-model指令，按照v-model的工作原理，
            组件必须通过props["value"]接收外界传值，必须通过发射input事件(如this.$emit("input", r))
            向外界传值
        -->
        <slider v-model="vol" ></slider>

        <p>{{vol.toFixed(1)}}</p>
    </div>
</body>
<script src="./vue.js"></script>
<script type="text/html" id="slider">
    <div class="slider-box" 
    @mousedown="mousedown" 
    @mousemove="mousemove" 
    @mouseup="mouseup"
    @mpuseleave="mouseup">
        <div class="slider-line" :style="lineStyle"></div>
        <div class="slider-point" :style="pointStyle"></div>
    </div>
</script>
<script>
    Vue.component("slider", {
        template: "#slider",
        props: ["value"],
         data: {
            isDown: false,
        },
        computed: {
            pointStyle: function () {
                var l = this.value * 360 + 20 - 10;

                l = l < 10 ? 10 : l;
                l = l > 370 ? 370 : l;
                return {
                    left: l + 'px'
                }
            },
            lineStyle:function(){
                var percent = this.value*100+'%';
                var bg= "linear-gradient(to right,red ,red "+percent+",blue "+percent+",blue)"
                return{
                    background:bg
                }
            }
        },
       
        methods: {
            mousedown: function (e) {
                var x = e.pageX - this.$el.offsetLeft;
                x = x < 20 ? 20 : x;
                x = x > 380 ? 380 : x;
                var r = (x - 20) / 360;
                this.$emit("ratiochange", r);
                this.isDown = true;
            },
            mousemove: function (e) {
                if (this.isDown) {
                    var x = e.pageX - this.$el.offsetLeft;
                    x = x < 20 ? 20 : x;
                    x = x > 380 ? 380 : x;
                    var r = (x - 20) / 360;
                    this.$emit("input", r);
                }
            },
            mouseup:function(e){
                this.isDown =false
            }
           
        }
    })

    new Vue({
        el: "#app",
        data: {
            vol: 0.3,
        },
        //methods 相当于vol= $event
        methods: {
            ratiochange: function (e) {
                this.vol = e
            }
        }
    });

</script>

</html>